package com.wwwarehouse.resource_center.adapter.createobject;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.res.ResBean;
import com.wwwarehouse.resource_center.eventbus_event.ResCategoryEvent;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Description: 工具分类
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/9/15 13:52
 */

public class ResCategoryAdapter extends RecyclerView.Adapter<ResCategoryAdapter.ViewHolder> {

    private Context mContext = null;
    private int mHeight = 0;
    private int mWidth = 0;
    private List<ResBean.DataBean> tagList = new ArrayList<>();
    public ResCategoryAdapter(Context context, List<ResBean.DataBean> list, int height, int Width) {
        this.mContext = context;
        this.mHeight = height;
        this.mWidth = Width;
        this.tagList = list;

    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_goods_category_select_obj_adapter, parent, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mTextView.getLayoutParams();
        params.width = (mWidth- ConvertUtils.dip2px(mContext,14*5))/4;//设置当前控件布局的宽度和margin
        params.height =params.width;
        holder.mTextView.setLayoutParams(params);
        holder.mTextView.setText(tagList.get(position).getDefinedName());
        holder.itemView.setTag(tagList.get(position));
        holder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.mTextView.setSelected(!holder.mTextView.isSelected());
                EventBus.getDefault().post(new ResCategoryEvent(tagList.get(position)));
            }
        });

    }

    @Override
    public int getItemCount() {
        return tagList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;

        public ViewHolder(View view) {
            super(view);
            mTextView = (TextView) view.findViewById(R.id.tv);
        }
    }
}
